/**
 * Created by Jaye on 2017/12/22.
 */
function refresh() {
    location.reload();
}
// 提交表单
function check_form() {
    var accesspoint_username = $.trim($('#accesspoint_username').val());
    var accesspoint_nickname = $.trim($('#accesspoint_nickname').val());
    var form_data = $('#form_data').serialize();
    $.ajax(
        {
            url: "accesspoint/create",
            data: form_data,
            type: "post",
            dataType: "json",
            beforeSend: function () {
                $("#tip").html("<span style='color:blue'>正在处理...</span>");
                return true;
            },
            success: function (data) {
                if (data.code == 1000) {
                    if (data.data.executeResult) {
                        swal({
                            title: "新增成功！",
                            type: "success"
                        });
                        location.reload();
                        return;
                    }
                }
                $("#tip").html("<span style='color:red'>失败，请重试</span>");
                swal({
                    title: "操作失败！",
                    type: "error"
                });
            },
            error: function () {
                swal({
                    title: "请求出错！",
                    type: "error"
                });
            },
            complete: function () {
                $('#acting_tips').hide();
            }
        });

    // return false;
}

$(function () {
    getOrgPeer();
});

function executeChaincode() {
    var chaincodeId = $('#chaincodeId').val();
    var chaincodeName = $('#chaincodeName').val();
    var chaincodeVersion = $('#chaincodeVersion').val();
    var chaincodePath = $('#chaincodePath').val();
    var channelName = $('#channelName').val();
    var channelLocation = "1";
    var fcn = $('#fcn').val();
    var args = $('#args').val().split("&");
    var channel1 = new channel(channelName);
    var chaincode1 = new chaincode(chaincodeName, chaincodeVersion, chaincodePath, channelLocation, fcn, args);

    var data = {"channel": channel1, "chainCode": chaincode1}
    var requestType = $('#requestType').val();
    var para = {"reqType": requestType, "data": JSON.stringify(data)};
    ajaxExecuteChaincode(para);
}
function chaincode(name, version, path, location, fcn, args) {
    this.name = name;
    this.version = version;
    this.path = path;
    this.location = location;
    this.fcn = fcn;
    this.args = args;
}
function channel(name) {
    this.name = name;
}
function ajaxExecuteChaincode(para) {
    $.ajax(
        {
            url: "chaincode/execute",
            data: para,
            type: "post",
            dataType: "json",
            // contentType: "application/json",
            success: function (data) {
                $('#result').val(JSON.stringify(data, null, "\t"));
                // if (data.code == 1000) {
                //     $('#result').val(data);
                // } else {
                //     swal({
                //         title: "执行Chaincode失败",
                //         type: "error",
                //         confirmButtonColor: "#DD6B55",
                //         confirmButtonText: "确认",
                //         closeOnConfirm: false
                //     });
                // }
            },
            error: function () {
                swal({
                    title: "请求出错",
                    type: "error",
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确认",
                    closeOnConfirm: false
                });
            }
        });
}
function getOrgPeer() {
    $.ajax(
        {
            url: "peer/list",
            data: {"data": ""},
            type: "post",
            dataType: "json",
            // contentType: "application/json",
            success: function (data) {
                if (data.code == 1000) {
                    setPeer(data.data);
                } else {
                    swal({
                        title: "查询节点出错,请检查",
                        type: "error",
                        confirmButtonColor: "#DD6B55",
                        confirmButtonText: "确认",
                        closeOnConfirm: false
                    });
                }
            },
            error: function () {
                swal({
                    title: "请求查询节点出错",
                    type: "error",
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "确认",
                    closeOnConfirm: false
                });
            }
        });
    setPeer();
}
function setPeer(peers) {
    var deviceStr = "";
    for (var i = 0; i < peers.length; i++) {
        deviceStr += "<option  data-icon='glyphicon glyphicon-heart' value='" + peers[i].peerId + "'> " + peers[i].peerName + "</option>";
    }
    $("#peerIds").html("");
    $('#peerIds').append(deviceStr);
    $('#peerIds').selectpicker('refresh');
}
function installChaincode() {
    var url = "chaincode/install";
    var form_data = $('#form_data').serialize();
    var peerList = $('#peerlist').val();
    ajaxRequest(url, form_data);
}
function instantiateChaincode() {
    var url = "chaincode/instantiate";
    var form_data = $('#form_data').serialize();
    ajaxRequest(url, form_data);
}
function ajaxRequest(url, form_data) {
    $.ajax(
        {
            url: url,
            data: form_data,
            type: "post",
            dataType: "json",
            async: false,
            success: function (data) {
                if (data.code == 1000) {
                    swal({
                        title: "操作成功！",
                        type: "success"
                    });
                    // location.reload();
                    return;
                }
                swal({
                    title: "操作失败！",
                    type: "error"
                });
            },
            error: function () {
                swal({
                    title: "请求出错！",
                    type: "error"
                });
            }
        });
}